import { BehaviorSubject, combineLatest, debounceTime } from "rxjs";
import 幻灵谷 from "./npcmap/幻灵谷.jpg";
import 魑休林 from "./npcmap/魑休林.jpg";
import 羽嘉森林 from "./npcmap/羽嘉森林.jpg";
import 积羽湖 from "./npcmap/积羽湖.jpg";
import 远古神殿  from "./npcmap/远古神殿.jpg";
import 埋骨之地 from "./npcmap/埋骨之地.jpg";
import 棋盘谷 from "./npcmap/棋盘谷.jpg";
import 离离草原 from "./npcmap/离离草原.jpg";
import 碎梦岛 from "./npcmap/碎梦岛.jpg";
import 聚蚁山 from "./npcmap/聚蚁山.jpg";
export const pics = {
  幻灵谷,
  魑休林,
  羽嘉森林,
  倚竹村郊外积羽湖:积羽湖,
  远古神殿郊外: 远古神殿,
  埋骨之地,
  棋盘谷,
  离离草原,
  碎梦岛,
  聚蚁山,
};
import {
  npcWords1,
  npcWords2,
  npcPinyi1,
  npcPinyin2,
  mapWords1,
  mapPinyin1,
  mapWords2,
  mapPinyin2,
  dataMap1,
  dataMap2,
} from "./data";
export const dataVersion = "1.0.0";
const getShortPinyin = (pinyin) => {
  return pinyin
    .split(" ")
    .map((i) => i[0])
    .join("");
};

/**
 *
 * @param {string} words
 * @param {string} pinyin
 * @returns {import('./index').Entity[]}
 */
const makeEntityList = (words, pinyin) => {
  const ws = words.split(",");
  const pys = pinyin.split(",");
  return ws.map((i, idx) => ({
    text: i,
    full: pys[idx].replace(/\s+/g, ""),
    short: getShortPinyin(pys[idx]),
  }));
};
const npcList1 = makeEntityList(npcWords1, npcPinyi1);
const npcList2 = makeEntityList(npcWords2, npcPinyin2);
const mapList1 = makeEntityList(mapWords1, mapPinyin1);
const mapList2 = makeEntityList(mapWords2, mapPinyin2);
const dataTable1 = dataMap1.map((i) =>
  i
    .split(/\s+/)
    .map((i) => i.trim())
    .filter((i) => Boolean(i))
);
const dataTable2 = dataMap2.map((i) =>
  i
    .split(/\s+/)
    .map((i) => i.trim())
    .filter((i) => Boolean(i))
);

/**
 *
 * @param {string} map
 * @param {string[][]} mapsArray
 * @param {import('./index').Entity[]} npcs
 */
const getNpcs = (map, mapsArray, npcs) => {
  return mapsArray
    .map((maps) => maps.indexOf(map))
    .filter((i) => i >= 0)
    .map((i) => npcs[i]);
};
const getNpcsPossible = (map) => {
  const npcs = getNpcs(map, dataTable1, npcList1);
  if (npcs.length > 0) {
    return npcs;
  }
  return getNpcs(map, dataTable2, npcList2);
};
export const currMap$ = new BehaviorSubject("");
export const allMaps = [...mapList1, ...mapList2];
export const currNpc$ = new BehaviorSubject("");

/**
 * @type {import('rxjs').BehaviorSubject<import('./index').Entity[]>}
 */
export const currNpcOptions$ = new BehaviorSubject([]);
export const destNpc$ = new BehaviorSubject("");

/**
 * @type {import('rxjs').BehaviorSubject<import('./index').Entity[]>}
 */
export const destNpcOptions$ = new BehaviorSubject([]);
export const destMap$ = new BehaviorSubject("");
currMap$.subscribe((map) => {
  currNpcOptions$.next(Boolean(map) ? getNpcsPossible(map) : []);
  destNpcOptions$.next([]);
  currNpc$.next("");
  destNpc$.next("");
  destMap$.next("");
});

currNpc$.subscribe((npc) => {
  destNpcOptions$.next(
    Boolean(npc) ? currNpcOptions$.value.filter((i) => i.text != npc) : []
  );
  destNpc$.next("");
  destMap$.next("");
});

/**
 *
 * @param {string} cm
 * @param {string} cn
 * @param {string} dn
 * @param {import('./index').Entity[]} npcs
 * @param {string[][]} mapsArray
 */
const getDestMap = (cm, cn, dn, npcs, mapsArray) => {
  const ci = npcs.findIndex((i) => i.text == cn);
  if (ci >= 0) {
    const da = mapsArray.find((i) => i[ci] == cm);
    const di = npcs.findIndex((i) => i.text == dn);
    if (di >= 0) {
      return da[di];
    }
  }
  return "";
};

destNpc$.subscribe((npc) => {
  if (!Boolean(npc)) {
    destMap$.next("");
    return;
  }
  const cm = currMap$.value;
  const cn = currNpc$.value;
  let dm = getDestMap(cm, cn, npc, npcList1, dataTable1);
  if (dm) {
    destMap$.next(dm);
    return;
  }
  dm = getDestMap(cm, cn, npc, npcList2, dataTable2);
  destMap$.next(dm);
});
